﻿Imports DTO
Public Class LoaiSachDAO
    Public Shared Function DanhSach_Loai_DAO(ByVal strSQL As String) As List(Of LoaiSachDTO)
        Dim kq As New List(Of LoaiSachDTO)()
        Dim datatable As DataTable = ketnoi.QR_SQL(strSQL)
        Dim loaisach As LoaiSachDTO
        Dim i = 0
        While (i < datatable.Rows.Count)
            loaisach = New LoaiSachDTO
            loaisach.MaLoaiSach = datatable.Rows(i)("MaLS").ToString
            loaisach.TenLoaiSach = datatable.Rows(i)("TenLS").ToString
            kq.Add(loaisach)
            i = i + 1
        End While
        Return kq
    End Function
    Public Shared Function Them_Loai_DAO(ByVal loaisach As LoaiSachDTO) As String
        Dim sql = ""
        If loaisach.MaLoaiSach = "" Then
            Return "Lỗi: Chưa Nhập Mã Sách"
        End If
        If loaisach.TenLoaiSach = "" Then
            'sql = "Insert Into LoaiSach(MaLS, TenLS) values('" + loaisach.MaLoaiSach + "',NULL)"
            'Hoặc có thể làm theo dạng:
            sql = String.Format("Insert Into LoaiSach(MaLoaiSach,TenLoaiSach) values('{0}',NULL)", loaisach.MaLoaiSach)
        Else
            sql = String.Format("Insert Into LoaiSach(MaLS,TenLS) values('{0}',N'{1}')", loaisach.MaLoaiSach, loaisach.TenLoaiSach)
        End If
        Return ketnoi.NonQR_SQL(sql)
    End Function
    Public Shared Function Sua_Loai_DAO(ByVal loaisach As LoaiSachDTO, ByVal MaCu As String) As String
        Dim sql = ""
        If loaisach.MaLoaiSach = "" Then
            Return "Lỗi: Chưa Nhập Mã Sách"
        End If
        If loaisach.TenLoaiSach = "" Then
            sql = String.Format("Update LoaiSach set MaLS='{0}', TenLS=NULL where MaLS='{1}'", loaisach.MaLoaiSach, MaCu)
        Else
            sql = String.Format("Update LoaiSach set MaLS='{0}', TenLS=N'{1}' where MaLS='{2}'", loaisach.MaLoaiSach, loaisach.TenLoaiSach, MaCu)
        End If
        Return ketnoi.NonQR_SQL(sql)
    End Function
    Public Shared Function Xoa_Loai_DAO(ByVal loaisach As LoaiSachDTO) As String
        Dim sql = "Delete From LoaiSach where MaLS='" + loaisach.MaLoaiSach + "'"
        Return ketnoi.NonQR_SQL(sql)
    End Function
End Class
